<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true">
				<!--<el-form-item>
					<el-input v-model="filters.nickName" placeholder="昵称" clearable></el-input>
				</el-form-item>-->
				
				<el-form-item>
					<el-input v-model="filters.userName" placeholder="账号" clearable></el-input>
				</el-form-item>
				<!--<el-form-item label="状态">
		            <el-select v-model="filters.status" clearable> 
		                <el-option v-for="item in optionStatus" :key="item.value" :label="item.label" :value="item.value" ></el-option>
		            </el-select>
				</el-form-item>-->
				<el-form-item>
					<el-button type="primary" size="medium" v-on:click="handleCurrentChange(1)">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" size="medium" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="list" stripe highlight-current-row border v-loading="listLoading" style="width: 100%;">
			<!--<el-table-column prop="nickName" label="昵称" min-width="80" align="center">
			</el-table-column>-->
			<el-table-column prop="username" label="账号" min-width="80" align="center">
			</el-table-column>
			<!--<el-table-column label="状态" min-width="80" align="center">
				<template slot-scope="scope">
					<span>{{scope.row.status==='0'?'正常' :'禁用'}}</span>
				</template>
			</el-table-column>-->
			<el-table-column label="操作" width="250" align="center">
				<template slot-scope="scope">
					<!--<el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>-->
					<el-button size="mini" @click="handleChongzhi(scope.$index, scope.row)">重置密码</el-button>
					<el-button type="danger" plain size="mini" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-pagination background layout="prev, pager, next, jumper" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
			</el-pagination>
		</el-col>
        <!--重置-->
		<el-dialog title="重置密码" :visible.sync="chongzhiFormVisible" :close-on-click-modal="false" width="500px">
			<el-form :model="chongzhiForm" label-width="100px" ref="chongzhiForm">
				<el-form-item label="新密码" prop="newPassword" :rules="[{ required: true, message: '请填写新密码', trigger: 'blur' }]">
					<el-input v-model="chongzhiForm.newPassword" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button size="mini" @click.native="chongzhiFormVisible = false">取消</el-button>
				<el-button size="mini" type="primary" @click.native="chongzhiSubmit" :loading="chongzhiLoading">提交</el-button>
			</div>
		</el-dialog>
		<!--编辑界面-->
		<el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false" width="500px">
			<el-form :model="editForm" label-width="100px" ref="editForm">
				<el-form-item label="昵称" prop="nickName">
					<el-input v-model="editForm.nickName" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="账号" prop="userName" :rules="[{ required: true, message: '请填写账号', trigger: 'blur' }]">
					<el-input v-model="editForm.userName" auto-complete="off"></el-input>
				</el-form-item>
				<!--<el-form-item label="密码" prop="password" :rules="[{ required: true, message: '请填写密码', trigger: 'blur' }]">
					<el-input v-model="editForm.password" auto-complete="off"></el-input>
				</el-form-item>-->
				<el-form-item label="状态" prop="status">
                    <el-select v-model="editForm.status">
                        <el-option v-for="item in optionStatus" :key="item.value" :label="item.label" :value="item.value"></el-option>
                    </el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button size="mini" @click.native="editFormVisible = false">取消</el-button>
				<el-button size="mini" type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>
		<!--新增界面-->
		<el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false" width="500px">
			<el-form :model="addForm" label-width="100px" ref="addForm">
				<!--<el-form-item label="昵称" prop="nickName">
					<el-input v-model="addForm.nickName" auto-complete="off"></el-input>
				</el-form-item>-->
				<el-form-item label="账号" prop="userName" :rules="[{ required: true, message: '请填写账号', trigger: 'blur' }]">
					<el-input v-model="addForm.userName" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="密码" prop="password" :rules="[{ required: true, message: '请填写密码', trigger: 'blur' }]">
					<el-input v-model="addForm.password" auto-complete="off"></el-input>
				</el-form-item>
				<!--<el-form-item label="状态" prop="status">
                    <el-select v-model="addForm.status">
                        <el-option v-for="item in optionStatus" :key="item.value" :label="item.label" :value="item.value"></el-option>
                    </el-select>
				</el-form-item>-->
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button size="mini" @click.native="addFormVisible = false">取消</el-button>
				<el-button size="mini" type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	
	export default {
		data() {
			return {
				filters: {
					nickName: '',
					status:'',
					userName:'',
				},
				data:[],
				list: [],
				total: 0,
				page: 1,
				pageSize: 10,
				option:[{
					value:'0',
					label:'总管理员',
				},{
					value:'1',
					label:'审核员',
				},{
					value:'2',
					label:'财务管理员'
				}],
				optionStatus:[{
					value:0,
					label:'正常'
				},{
					value:1,
					label:'禁用'
				}],
				listLoading: false,//加载条是否显示
				editFormVisible: false,//编辑的弹框是否显示
				editLoading: false,
				editForm: {
					nickName: '',
					userName: '',
					password:'',
					status:''
				},
				addFormVisible: false,//新增的弹框是否显示
				addLoading: false,//提交的loading
				addForm: {
					userId:'',
					nickName: '',
					userName: '',
					password:'',
					status:''
				},
				chongzhiFormVisible: false,//编辑的弹框是否显示
				chongzhiLoading: false,
				chongzhiForm: {
					newPassword: '',
					userId: '',
				},
			}
		},
		methods: {
			//类型
			type: function (row, column) {
				return row.type === 0 ? '总管理员' :row.type === 1 ? '审核员' :row.type === 2 ? '财务管理员' : '未知';
			},
			handleCurrentChange(val) {
				this.page = val;
				this.getUsers();
			},
			//获取列表
			getUsers:function (){
				
				this.listLoading = true;

				let para = {
					page: this.page,
					size: this.pageSize,
					status:this.filters.status,
					nickName:this.filters.nickName,
					userName:this.filters.userName,
				};
				var instance = this.$http.create({headers: {'content-type': 'application/x-www-form-urlencoded','admin_five_palace_token':JSON.parse(sessionStorage.getItem('token'))},
				});  
				instance.post('admin/account/manage/list', this.qs.stringify(para)).then(data => {
					
					let  res = data.data;
					if (res.code !== 0) {
						this.$message({
							message: res.msg,
							type: 'error'
						});
					} else {
						this.total = res.total;
						this.list = res.rows;
						this.listLoading = false;
					}
				});
			},
			//显示新增界面
			handleAdd: function () {
				this.addFormVisible = true;
				this.addForm = {
					nickName: '',
					userName: '',
					password:'',
					status:''
				};
			},
			//新增
			addSubmit: function () {
				//addForm 弹框的名字 去掉this.$refs.addForm.validate((valid))这个语句也行
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							let para = Object.assign({}, this.addForm);
							var instance = this.$http.create({headers: {'content-type': 'application/x-www-form-urlencoded','admin_five_palace_token':JSON.parse(sessionStorage.getItem('token'))},
							});  
							instance.post('admin/account/manage/addUser', this.qs.stringify(para)).then(data => {
								
								let  res = data.data;
								if (res.code !== 0) {
									this.$message({
										message: res.msg,
										type: 'error'
									});
								} else {
									this.addLoading = false;
									this.$message({
										message: '提交成功',
										type: 'success'
									});
									this.$refs['addForm'].resetFields();
									this.addFormVisible = false;
									this.getUsers();
								}
							});
						});
					}
				});
			},
			//显示编辑界面
			handleEdit: function (index ,row) {
				this.editFormVisible = true;
				this.editForm.userId = row.userId
				this.editForm.userName = row.userName;
				this.editForm.password = row.password;
				this.editForm.status = JSON.parse(row.status);
				this.editForm.nickName = row.nickName;
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							let para = {
								userId:this.editForm.userId,
								userName:this.editForm.userName,
								password:this.editForm.password,
								nickName:this.editForm.nickName,
								status:this.editForm.status,
							}
							var instance = this.$http.create({headers: {'content-type': 'application/x-www-form-urlencoded','admin_five_palace_token':JSON.parse(sessionStorage.getItem('token'))},
							});  
							instance.post('admin/account/manage/updUser', this.qs.stringify(para)).then(data => {
								
								let  res = data.data;
								if (res.code !== 0) {
									this.$message({
										message: res.msg,
										type: 'error'
									});
								} else {
									this.editLoading = false;
									this.$message({
										message: '提交成功',
										type: 'success'
									});
									this.$refs['editForm'].resetFields();
									this.editFormVisible = false;
									this.getUsers();
								}
							});
						});
					}
				});
			},
			//显示重置界面
			handleChongzhi: function (index ,row) {
				this.chongzhiFormVisible = true;
				this.chongzhiForm.userId = row.id
			},
			//编辑
			chongzhiSubmit: function () {
				this.$refs.chongzhiForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.chongzhiLoading = true;
							let para = {
								userId:this.chongzhiForm.userId,
								newPassword:this.chongzhiForm.newPassword,
							}
							var instance = this.$http.create({headers: {'content-type': 'application/x-www-form-urlencoded','admin_five_palace_token':JSON.parse(sessionStorage.getItem('token'))},
							});  
							instance.post('admin/account/manage/resetPasswords', this.qs.stringify(para)).then(data => {
								
								let  res = data.data;
								if (res.code !== 0) {
									this.$message({
										message: res.msg,
										type: 'error'
									});
								} else {
									this.chongzhiLoading = false;
									this.$message({
										message: '提交成功',
										type: 'success'
									});
									this.$refs['chongzhiForm'].resetFields();
									this.chongzhiFormVisible = false;
								}
							});
						});
					}
				});
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					let para = { userId: row.id };
					var instance = this.$http.create({headers: {'content-type': 'application/x-www-form-urlencoded','admin_five_palace_token':JSON.parse(sessionStorage.getItem('token'))},
					});  
					instance.post('admin/account/manage/deleteUser', this.qs.stringify(para)).then(data => {
						
						let  res = data.data;
						if (res.code !== 0) {
							this.$message({
								message: res.msg,
								type: 'error'
							});
						} else {
							this.$message({
								message: '删除成功',
								type: 'success'
							});
							this.getUsers();
						}
					})
				}).catch(() => {

				});
			}
		},
		mounted() {
			this.getUsers();
		}
	}

</script>

<style scoped>

</style>